<?php
/**
 * Easthv Zend Framework Project
 *
 *
 * LICENSE:
 *
 * @category   Zend
 * @package    Zend_Helper
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
*/

/**
 * I18N Helper
 * 1.Get default language from Controller
 * 2.Parse I18N XML Files
 * 3.Replace String to I18N
 *
 * @category   Zend
 * @package    Zend_Helper
 * @copyright  Copyright (c) 2009-2010 Easthv Team @ East Hui Van Business Consulting Co., Ltd.(http://www.easthv.com)
 * @license    http://framework.zend.com/license/   BSD License
 * @version    1.0
 * @link       http://www.easthv.com
 * @since      1.0
 * @deprecated
 */
class Zend_View_Helper_I18n
{
    /**
     * @var object Zend_View
     */
    public $view;

    /**
     * @var string Current I18N File
     */
    private $_transFile;

    /**
     * @var mixed If I18N disabled, this will be Zend_View_Helper_I18N itself
     *            If I18N enabled, this will be  Zend_Translate
     */    
	private $_i18n;


    /**
     * Helper core function
     *
     *
     * @param  string $text
     *
     * @access public
     *
     * @return string translated text
     */
    public function I18n($text = null)
    {
        if (0 == func_num_args()) {
            return $this;
        }

        return $this->_i18n->_($text);
    }

    /**
     * Set Zend View
     *
     *
     * @param  Zend_View_Interface   $view   zend view interface 
     *
     * @access public
     *
     * @return void
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;


        if (!$this->view->config['i18n']['enable']) {
            return $this->_i18n = $this;
        }

        if (!$this->view->language) {
            $this->view->language = $this->view->config['i18n']['default'];
        }

        $path = $this->view->config['path']['i18n']; 

        $controller = $this->view->params['controller'];
        $action = $this->view->params['action'];
        
        //Load main trans file
        $this->_transFile = APPLICATION_PATH . $path . $this->view->config['i18n']['main'];

        //main file isn't exist, i18n will be disable
        if (!file_exists($this->_transFile)) {
            return $this->_i18n = $this;
		}

		//set cache
		if ($this->view->config['cache']['i18n']) {

			$cache = Zend_Cache::factory('Core', 'File',array(
				'lifeTime' => 7200,
				'automatic_serialization' => true
			),array(
				'cache_dir' => APPLICATION_PATH . $this->view->config['path']['cache']['i18n']
			));
			Zend_Translate::setCache($cache);

		}

		$this->_i18n = new Zend_Translate('tmx', $this->_transFile, 'en');

		$local = $this->view->render ? $this->view->render : $action;
		$local = APPLICATION_PATH . $path . $controller . '/' . $local . '.xml';

		if (file_exists($local)) {
			$this->_i18n->getAdapter()->addTranslation($local, 'en');
		}

		//All follow tmx files must be setting in ini file
		if ($this->view->config['i18n']['success'] && $this->view->success) {
			$success = APPLICATION_PATH . $path . $config['system']['i18n']['success'];
			$this->_i18n->getAdapter()->addTranslation($success, 'en');
        }


        if ($this->view->config['i18n']['errors'] && $this->view->errors) {
            $errors = APPLICATION_PATH . $path . $config['system']['i18n']['errors'];
            $this->_i18n->getAdapter()->addTranslation($errors, 'en');
        }

        if ($this->view->config['i18n']['valids'] && $this->view->valids) {
            $valids = APPLICATION_PATH . $path . $config['system']['i18n']['valids'];
            $this->_i18n->getAdapter()->addTranslation($valids, 'en');
        }

        if ($this->view->config['i18n']['hint'] && $this->view->hint) {
            $hint = APPLICATION_PATH . $path . $config['system']['i18n']['hint'];
            $this->_i18n->getAdapter()->addTranslation($hint, 'en');
        }

        $this->_i18n->setOptions(array('disableNotices' => true));

		$xmlSupportLang = $this->_mappingLanguage($this->view->language);

        $this->_i18n->setLocale($xmlSupportLang);
	}

    /**
     * Mapping ISO 639-2 language code to rfc3066 language code (HTML / XML)
     *
     * @param  string
     *
     * @access private
     *
     * @return string
     */
	private function _mappingLanguage($isoLang)
	{
		$mapping = array(
			'en' => 'en',
			'zh' => 'zh-CN',	
		);

		return $mapping[$isoLang] ? $mapping[$isoLang] : 'en';
	}


    /**
     * This function is as same as Zend_Translate::_()
     * Only use when i18n disabled
     *
     * @param  string $text
     *
     * @access private
     *
     * @return string text itself
     */
    private function _($text = null)
    {
        return $text;
    }


    /**
     * Translate by Zend_Locale
     *
     * @param  string $text
     * @param  string $tranType Zend TranslationList type (see http://framework.zend.com/manual/en/zend.locale.functions.html)
     * @param  string $lang
	 * 
     * @return string
     */
	public function trans($text, $tranType, $lang = null)
	{
		$lang = $lang ? $lang : $this->view->language;

		return Zend_Locale::getTranslation($text, $tranType, $lang);
	}

}
